package tw.idv.liuken.mediacodectest;

import java.nio.ByteBuffer;

import android.app.Activity;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecInfo.CodecCapabilities;
import android.media.MediaCodecInfo.CodecProfileLevel;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class MediaCodecActivity extends Activity {	
	TextView tv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	int mediaCodecCount;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_codec);
        tv = (TextView)findViewById(R.id.textview);
        tv.setMovementMethod(ScrollingMovementMethod.getInstance());
        
        mediaCodecCount = MediaCodecList.getCodecCount();
        tv.setText("Media Codec Count = " + mediaCodecCount + "\n");
        
        int i;
        for (i=0 ; i<6 ; i++) {
        	MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        	CodecCapabilities cap = codecInfo.getCapabilitiesForType(codecInfo.getName());
        	tv.append(codecInfo.getName() + "\n");
        	tv.append("\tEncoder: " + codecInfo.isEncoder() + "\n");
        	
        	tv.append("\tSupported Type:\n");
        	String[] supportedType = codecInfo.getSupportedTypes();
        	if (supportedType.length > 0) {
        		for (String type : supportedType) {
        			tv.append("\t\t" + type + ", ");
        		}
        		tv.append("\n");
        	}
        	
        	tv.append("\tCapabilities:\n");
        	
        	if (cap.colorFormats.length > 0) {
	        	tv.append("\t\tColor Formats:\n\t\t\t");
	        	for (int color : cap.colorFormats) {
	        		tv.append(getColorFormat(color) + ", ");
	        	}
	        	tv.append("\n");
        	}
        	
        	if (cap.profileLevels.length > 0) {
        		tv.append("\t\tProfile Level:\n\t\t\t");
	        	for (CodecProfileLevel pl : cap.profileLevels) {
	        		tv.append("(Profile=" + getProfile(codecInfo.getName(), pl.profile) + " Level=" + getLevel(codecInfo.getName(), pl.level) + "), ");
	        	}
	        	tv.append("\n");
        	}
        	
        	tv.append("===========================================================\n");
        }
        /*
        MediaCodec codec = MediaCodec.createEncoderByType("video/avc");
        MediaFormat format = new MediaFormat();
        format.setString(MediaFormat.KEY_MIME, "video/avc");
        format.setInteger(MediaFormat.KEY_BIT_RATE, 4096);
        format.setInteger(MediaFormat.KEY_WIDTH, 1280);
        format.setInteger(MediaFormat.KEY_HEIGHT, 720);
        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
        format.setInteger(MediaFormat.KEY_FRAME_RATE, 25);
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 25);
        */
        //codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        //codec.start();
        
        //ByteBuffer[] inputBuffers = codec.getInputBuffers();
        //ByteBuffer[] outputBuffers = codec.getOutputBuffers();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_media_codec, menu);
        return true;
    }
    
    public String getColorFormat(int color) {
    	switch (color) {
    	case CodecCapabilities.COLOR_FormatMonochrome:
    		return "COLOR_FormatMonochrome";
    	case CodecCapabilities.COLOR_Format8bitRGB332:
    		return "COLOR_Format8bitRGB332";
    	case CodecCapabilities.COLOR_Format12bitRGB444:
    		return "COLOR_Format12bitRGB444";
    	case CodecCapabilities.COLOR_Format16bitARGB4444:
    		return "COLOR_Format16bitARGB4444";
    	case CodecCapabilities.COLOR_Format16bitARGB1555:
    		return "COLOR_Format16bitARGB1555";
    	case CodecCapabilities.COLOR_Format16bitRGB565:
    		return "COLOR_Format16bitRGB565";
    	case CodecCapabilities.COLOR_Format16bitBGR565:
    		return "COLOR_Format16bitBGR565";
    	case CodecCapabilities.COLOR_Format18bitRGB666:
    		return "COLOR_Format18bitRGB666";
    	case CodecCapabilities.COLOR_Format18bitARGB1665:
    		return "COLOR_Format18bitARGB1665";
    	case CodecCapabilities.COLOR_Format19bitARGB1666:
    		return "COLOR_Format19bitARGB1666";
    	case CodecCapabilities.COLOR_Format24bitRGB888:
    		return "COLOR_Format24bitRGB888";
    	case CodecCapabilities.COLOR_Format24bitBGR888:
    		return "COLOR_Format24bitBGR888";
    	case CodecCapabilities.COLOR_Format24bitARGB1887:
    		return "COLOR_Format24bitARGB1887";
    	case CodecCapabilities.COLOR_Format25bitARGB1888:
    		return "COLOR_Format25bitARGB1888";
    	case CodecCapabilities.COLOR_Format32bitBGRA8888:
    		return "COLOR_Format32bitBGRA8888";
    	case CodecCapabilities.COLOR_Format32bitARGB8888:
    		return "COLOR_Format32bitARGB8888";
    	case CodecCapabilities.COLOR_FormatYUV411Planar:
    		return "COLOR_FormatYUV411Planar";
    	case CodecCapabilities.COLOR_FormatYUV411PackedPlanar:
    		return "COLOR_FormatYUV411PackedPlanar";
    	case CodecCapabilities.COLOR_FormatYUV420Planar:
    		return "COLOR_FormatYUV420Planar";
    	case CodecCapabilities.COLOR_FormatYUV420PackedPlanar:
    		return "COLOR_FormatYUV420PackedPlanar";
    	case CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
    		return "COLOR_FormatYUV420SemiPlanar";
    	case CodecCapabilities.COLOR_FormatYUV422Planar:
    		return "COLOR_FormatYUV422Planar";
    	case CodecCapabilities.COLOR_FormatYUV422PackedPlanar:
    		return "COLOR_FormatYUV422PackedPlanar";
    	case CodecCapabilities.COLOR_FormatYUV422SemiPlanar:
    		return "COLOR_FormatYUV422SemiPlanar";
    	case CodecCapabilities.COLOR_FormatYCbYCr:
    		return "COLOR_FormatYCbYCr";
    	case CodecCapabilities.COLOR_FormatYCrYCb:
    		return "COLOR_FormatYCrYCb";
    	case CodecCapabilities.COLOR_FormatCbYCrY:
    		return "COLOR_FormatCbYCrY";
    	case CodecCapabilities.COLOR_FormatCrYCbY:
    		return "COLOR_FormatCrYCbY";
    	case CodecCapabilities.COLOR_FormatYUV444Interleaved:
    		return "COLOR_FormatYUV444Interleaved";
    	case CodecCapabilities.COLOR_FormatRawBayer8bit:
    		return "COLOR_FormatRawBayer8bit";
    	case CodecCapabilities.COLOR_FormatRawBayer10bit:
    		return "COLOR_FormatRawBayer10bit";
    	case CodecCapabilities.COLOR_FormatRawBayer8bitcompressed:
    		return "COLOR_FormatRawBayer8bitcompressed";
    	case CodecCapabilities.COLOR_FormatL2:
    		return "COLOR_FormatL2";
    	case CodecCapabilities.COLOR_FormatL4:
    		return "COLOR_FormatL4";
    	case CodecCapabilities.COLOR_FormatL8:
    		return "COLOR_FormatL8";
    	case CodecCapabilities.COLOR_FormatL16:
    		return "COLOR_FormatL16";
    	case CodecCapabilities.COLOR_FormatL24:
    		return "COLOR_FormatL24";
    	case CodecCapabilities.COLOR_FormatL32:
    		return "COLOR_FormatL32";
    	case CodecCapabilities.COLOR_FormatYUV420PackedSemiPlanar:
    		return "COLOR_FormatYUV420PackedSemiPlanar";
    	case CodecCapabilities.COLOR_FormatYUV422PackedSemiPlanar:
    		return "COLOR_FormatYUV422PackedSemiPlanar";
    	case CodecCapabilities.COLOR_Format18BitBGR666:
    		return "COLOR_Format18BitBGR666";
    	case CodecCapabilities.COLOR_Format24BitARGB6666:
    		return "COLOR_Format24BitARGB6666";
    	case CodecCapabilities.COLOR_Format24BitABGR6666:
    		return "COLOR_Format24BitABGR6666";
    	case CodecCapabilities.COLOR_TI_FormatYUV420PackedSemiPlanar:
    		return "COLOR_TI_FormatYUV420PackedSemiPlanar";
    	case CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar:
    		return "COLOR_QCOM_FormatYUV420SemiPlanar";
    	default:
    		return color +"";
    	}
    };
    
    public String getProfile(String codec, int profile) {
    	if (codec.contains("h263")) {
    		switch(profile) {
    		case CodecProfileLevel.H263ProfileBaseline:
    			return "H263ProfileBaseline";
    		case CodecProfileLevel.H263ProfileH320Coding:
    			return "H263ProfileH320Coding";
    		case CodecProfileLevel.H263ProfileBackwardCompatible:
    			return "H263ProfileBackwardCompatible";
    		case CodecProfileLevel.H263ProfileISWV2:
    			return "H263ProfileISWV2";
    		case CodecProfileLevel.H263ProfileISWV3:
    			return "H263ProfileISWV3";
    		case CodecProfileLevel.H263ProfileHighCompression:
    			return "H263ProfileHighCompression";
    		case CodecProfileLevel.H263ProfileInternet:
    			return "H263ProfileInternet";
    		case CodecProfileLevel.H263ProfileInterlace:
    			return "H263ProfileInterlace";
    		case CodecProfileLevel.H263ProfileHighLatency:
    			return "H263ProfileHighLatency";
    		}
    	} else if (codec.contains("h264")) {
    		switch(profile) {
    		case CodecProfileLevel.AVCProfileBaseline:
    			return "AVCProfileBaseline";
    		case CodecProfileLevel.AVCProfileMain:
    			return "AVCProfileMain";
    		case CodecProfileLevel.AVCProfileExtended:
    			return "AVCProfileExtended";
    		case CodecProfileLevel.AVCProfileHigh:
    			return "AVCProfileHigh";
    		case CodecProfileLevel.AVCProfileHigh10:
    			return "AVCProfileHigh10";
    		case CodecProfileLevel.AVCProfileHigh422:
    			return "AVCProfileHigh422";
    		case CodecProfileLevel.AVCProfileHigh444:
    			return "AVCProfileHigh444";
    		}
    	} else if (codec.contains("mpeg4") || codec.contains("mp4")) {
    		switch(profile) {
    		case CodecProfileLevel.MPEG4ProfileSimple:
    			return "MPEG4ProfileSimple";
    		case CodecProfileLevel.MPEG4ProfileSimpleScalable:
    			return "MPEG4ProfileSimpleScalable";
    		case CodecProfileLevel.MPEG4ProfileCore:
    			return "MPEG4ProfileCore";
    		case CodecProfileLevel.MPEG4ProfileMain:
    			return "MPEG4ProfileMain";
    		case CodecProfileLevel.MPEG4ProfileNbit:
    			return "MPEG4ProfileNbit";
    		case CodecProfileLevel.MPEG4ProfileScalableTexture:
    			return "MPEG4ProfileScalableTexture";
    		case CodecProfileLevel.MPEG4ProfileSimpleFace:
    			return "MPEG4ProfileSimpleFace";
    		case CodecProfileLevel.MPEG4ProfileSimpleFBA:
    			return "MPEG4ProfileSimpleFBA";
    		case CodecProfileLevel.MPEG4ProfileBasicAnimated:
    			return "MPEG4ProfileBasicAnimated";
    		case CodecProfileLevel.MPEG4ProfileHybrid:
    			return "MPEG4ProfileHybrid";
    		case CodecProfileLevel.MPEG4ProfileAdvancedRealTime:
    			return "MPEG4ProfileAdvancedRealTime";
    		case CodecProfileLevel.MPEG4ProfileCoreScalable:
    			return "MPEG4ProfileCoreScalable";
    		case CodecProfileLevel.MPEG4ProfileAdvancedCoding:
    			return "MPEG4ProfileAdvancedCoding";
    		case CodecProfileLevel.MPEG4ProfileAdvancedCore:
    			return "MPEG4ProfileAdvancedCore";
    		case CodecProfileLevel.MPEG4ProfileAdvancedScalable:
    			return "MPEG4ProfileAdvancedScalable";
    		case CodecProfileLevel.MPEG4ProfileAdvancedSimple:
    			return "MPEG4ProfileAdvancedSimple";
    		}
    	}
    	return profile + "";
    }
    
    public String getLevel(String codec, int level) {
		if (codec.contains("h263")) {
			switch (level) {
			case CodecProfileLevel.H263Level10:
				return "H263Level10";
			case CodecProfileLevel.H263Level20:
				return "H263Level20";
			case CodecProfileLevel.H263Level30:
				return "H263Level30";
			case CodecProfileLevel.H263Level40:
				return "H263Level40";
			case CodecProfileLevel.H263Level50:
				return "H263Level50";
			case CodecProfileLevel.H263Level60:
				return "H263Level60";
			case CodecProfileLevel.H263Level70:
				return "H263Level70";
			}
		} else if (codec.contains("h264")) {
			switch (level) {
			case CodecProfileLevel.AVCLevel1:
				return "AVCLevel1";
			case CodecProfileLevel.AVCLevel1b:
				return "AVCLevel1b";
			case CodecProfileLevel.AVCLevel11:
				return "AVCLevel11";
			case CodecProfileLevel.AVCLevel12:
				return "AVCLevel12";
			case CodecProfileLevel.AVCLevel13:
				return "AVCLevel13";
			case CodecProfileLevel.AVCLevel2:
				return "AVCLevel2";
			case CodecProfileLevel.AVCLevel21:
				return "AVCLevel21";
			case CodecProfileLevel.AVCLevel22:
				return "AVCLevel22";
			case CodecProfileLevel.AVCLevel3:
				return "AVCLevel3";
			case CodecProfileLevel.AVCLevel31:
				return "AVCLevel31";
			case CodecProfileLevel.AVCLevel32:
				return "AVCLevel32";
			case CodecProfileLevel.AVCLevel4:
				return "AVCLevel4";
			case CodecProfileLevel.AVCLevel41:
				return "AVCLevel41";
			case CodecProfileLevel.AVCLevel42:
				return "AVCLevel42";
			case CodecProfileLevel.AVCLevel5:
				return "AVCLevel5";
			case CodecProfileLevel.AVCLevel51:
				return "AVCLevel51";
			}
		} else if (codec.contains("mpeg4") || codec.contains("mp4")) {
			switch (level) {
			case CodecProfileLevel.MPEG4Level0:
				return "MPEG4Level0";
			case CodecProfileLevel.MPEG4Level0b:
				return "MPEG4Level0b";
			case CodecProfileLevel.MPEG4Level1:
				return "MPEG4Level1";
			case CodecProfileLevel.MPEG4Level2:
				return "MPEG4Level2";
			case CodecProfileLevel.MPEG4Level3:
				return "MPEG4Level3";
			case CodecProfileLevel.MPEG4Level4:
				return "MPEG4Level4";
			case CodecProfileLevel.MPEG4Level4a:
				return "MPEG4Level4a";
			case CodecProfileLevel.MPEG4Level5:
				return "MPEG4Level5";
			}
		}
		return level + "";
	}
}
